﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using IOP.Extension.DependencyInjection;
using IOP.ISEA;
using Silk.NET.Vulkan;

namespace VkSample127
{
    [ModulePriority(ModulePriority.Assets)]
    [ParentModule(typeof(PipelineModule))]
    public class AssetsModule : VulkanModule, IVulkanRenderWapperModule
    {
        public VulkanRenderWapper RenderWapper { get; set; }
        [Autowired]
        private ILogger<AssetsModule> _Logger { get; set; }

        protected override async Task Load(VulkanGraphicsManager graphicsManager)
        {
            try
            {
                var basePath = AppContext.BaseDirectory;
                var lDevice = graphicsManager.VulkanDevice;

                if (Parent is not PipelineModule pipelineModule) throw new NullReferenceException("Parent Module lost");
                ISEADocument document = new();
                await document.Load(Path.Combine(AppContext.BaseDirectory, "test127.isea"));
                var renders = await graphicsManager.CreateRenderObjectsFromISEA<RenderObject>(document);

                var flatData = new FlatData();
                flatData.GenVertexData();
                var vro = graphicsManager.CreateEmptyMesh<VRO>("Flat").
                    CreateVecticesBuffer(flatData.Data, (uint)flatData.VCount, lDevice, SharingMode.Exclusive);
                var obj = renders.Where(x => x.Name == "obj").First();
                var obj1 = renders.Where(x => x.Name == "obj1").First();
                var obj2 = renders.Where(x => x.Name == "obj2").First();
                var obj3 = renders.Where(x => x.Name == "obj3").First();
                obj.AddComponent(vro);
                obj1.AddComponent(vro);
                obj2.AddComponent(vro);
                obj3.AddComponent(vro);
                obj.GetTransform().Position = new Vector3(-3.3f, -3.3f, 0.0f);
                obj1.GetTransform().Position = new Vector3(3.3f, -3.3f, 0.0f);
                obj2.GetTransform().Position = new Vector3(-3.3f, 3.3f, 0.0f);
                obj3.GetTransform().Position = new Vector3(3.3f, 3.3f, 0.0f);

                var commonArchetype = ContextManager.CreateArchetype("Common", new TransformComponent());
                ContextManager.CreateContexts(1, commonArchetype, pipelineModule.CommonGroup, obj, false);
                ContextManager.CreateContexts(1, commonArchetype, pipelineModule.CommonGroup, obj2, false);
                ContextManager.CreateContexts(1, commonArchetype, pipelineModule.SharpeningGroup, obj1, false);
                ContextManager.CreateContexts(1, commonArchetype, pipelineModule.SharpeningGroup, obj3, false);
            }
            catch (Exception e)
            {
                _Logger?.LogError(e.Message + "\r\n" + e.StackTrace);
            }
        }

        protected override Task Unload(VulkanGraphicsManager manager)
        {
            return Task.CompletedTask;
        }
    }
}
